package DMSXL._2024.BackTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class partition {
    public static void main(String[] args) {

    }
    List<List<String>> result = new ArrayList<>();
    LinkedList<String> temp_result = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backTracking(s,0);
        return result;
    }

    public void backTracking(String s, int startIndex){
        if(startIndex == s.length()){
            result.add(new ArrayList<>(temp_result));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if(isValid(s,startIndex,i)){
                temp_result.add(s.substring(startIndex,i + 1));
            }else {
                continue;
            }
            backTracking(s,i + 1);
            temp_result.removeLast();
        }
    }

    public boolean isValid(String s, int start, int end){
        while (start < end){
            if(s.charAt(start++) != s.charAt(end--)){
                return false;
            }
        }
        return true;
    }
}
